package com.liuzhengting.common.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.HashMap;
import java.util.Map;


@JsonIgnoreProperties(ignoreUnknown = true)
public class GeneralResult
{
    // 结果状态, true 成功 false 失败
    private Boolean resultStatus;

    // 如果错误则返回 错误代码
    private String errorCode;

    // 错误详情描述
    private String errorMessage;

    // 结果为true时的 业务数据，可为空
    private Object resultData;

    private Map<String, Object> resMap;

    public GeneralResult(){
    }

    public GeneralResult(ErrorCode error){
        this(Boolean.FALSE, error.getCode(), error.getMsg());
    }

    public GeneralResult(ErrorCode error, Object obj){
        this(Boolean.FALSE, error.getCode(), error.getMsg());
        this.resultData = obj;
    }

    public GeneralResult(String errorCode, String errorMessage)
    {
        super();
        this.resultStatus = Boolean.FALSE;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public GeneralResult(Boolean resultStatus, String errorCode, String errorMessage)
    {
        super();
        this.resultStatus = resultStatus;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public GeneralResult ok()
    {
        this.resultStatus = Boolean.TRUE;
        return this;
    }

    public GeneralResult error(ErrorCode errCode)
    {
        this.errorCode = errCode.getCode();
        this.errorMessage = errCode.getMsg();
        this.resultStatus = Boolean.FALSE;
        return this;
    }

    public GeneralResult response(Object resultData)
    {
        this.resultData = resultData;
        this.resultStatus = Boolean.TRUE;
        return this;
    }

    public GeneralResult responseMap()
    {
        this.resMap = new HashMap<String, Object>();
	    this.resultData = this.resMap;
        this.resultStatus = Boolean.TRUE;
        return this;
    }

    public GeneralResult addParam(String key, Object value)
    {
        this.resMap.put(key, value);
        return this;
    }

    public GeneralResult addParams(Map<String, Object> params)
    {
        this.resMap.putAll(params);
        return this;
    }

    public Boolean getResultStatus()
    {
        return resultStatus;
    }

    public String getErrorCode()
    {
        return errorCode;
    }

    public String getErrorMessage()
    {
        return errorMessage;
    }

    public Object getResultData()
    {
        return resultData;
    }

    public void setResultStatus(Boolean resultStatus) {
        this.resultStatus = resultStatus;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setResultData(Object resultData) {
        this.resultData = resultData;
    }

    public void setResMap(Map<String, Object> resMap) {
        this.resMap = resMap;
    }

    public void setErr(ErrorCode errCode) {
        this.errorCode = errCode.getCode();
        this.errorMessage = errCode.getMsg();
        this.resultStatus = false;
    }
	
	@Override
	public String toString() {
		return "GeneralResult{" +
				"resultStatus=" + resultStatus +
				", errorCode='" + errorCode + '\'' +
				", errorMessage='" + errorMessage + '\'' +
				", resultData=" + resultData +
				", resMap=" + resMap +
				'}';
	}

}
